package com.gm.tool.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mouse on 2016/9/30.
 */

public class BytesUtil {


    //校验字节(异或)
    public static byte getCheckByte(byte[] bytes) {
        byte b = 0;
        for (int i = 0; i < bytes.length; i++)
            b ^= bytes[i];
        return b;
    }

    //获得校验字节
    public static byte getCheckByte(byte[]... bytelist) {

        byte result = 0;
        for (int i = 0; i < bytelist.length; i++) {
            if (bytelist[i] != null) {
                result ^= BytesUtil.getCheckByte(bytelist[i]);
            }
        }
        return result;
    }

    //字节打印字符串
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();

    }

    //bitmap转字节数组
    public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = null;
        byte[] result = null;
        try {
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
            result = baos.toByteArray();
        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //字节数组转bitmap
    public static Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    //字节拷贝
    public static void byteArrayCopy(final byte[] tag, byte[] src) {
        for (int i = 0; i < tag.length; i++) {
            tag[i] = src[i];
        }
    }

    //字节转对象
    public static Object byteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return obj;
    }


    //对象转字节
    public static byte[] objectToByte(Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return bytes;
    }

    //字节数组分隔
    public static List<byte[]> split(byte[] data, int... lens) {

        List<byte[]> list = null;
        int len = 0;

        for (int i : lens)
            len += i;

        if (len == data.length) {
            ByteBuffer bb = ByteBuffer.allocate(len).put(data);
            bb.clear();
            list = new ArrayList<>();
            for (int i = 0; i < lens.length; i++) {
                byte[] dst = new byte[lens[i]];
                bb.get(dst);
                list.add(dst);
            }
        }
        return list;
    }

    //合并
    public static byte[] merged(byte[] b1, byte[] b2) {
        byte[] b3 = new byte[b1.length + b2.length];
        System.arraycopy(b1, 0, b3, 0, b1.length);
        System.arraycopy(b2, 0, b3, b1.length, b2.length);
        return b3;
    }


    //long转bytes
    public static byte[] longToBytes(long num) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[i] = (byte) (num >>> (56 - (i * 8)));
        }
        return b;
    }

    //long转bytes
    public static byte[] longToBytes(long num, int beginIndex) {
        if (beginIndex < 0 || beginIndex > 7)
            return new byte[0];

        byte[] tag = new byte[8 - beginIndex];
        System.arraycopy(longToBytes(num), beginIndex, tag, 0, tag.length);
        return tag;
    }

    //bytes转long
    public static long bytesToLong(byte[] b) {
        long temp = 0;
        long res = 0;
        for (int i = 0; i < b.length; i++) {
            res <<= 8;
            temp = b[i] & 0xff;
            res |= temp;
        }
        return res;
    }


}
